//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <fcntl.h>
#include <unistd.h>

#include <iostream>

#include "ModelsSummaryByLigand.h"
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#define MESSAGE 0
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
using namespace std;
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
int main (int argc, char** argv) {
  
  String* filenameIn  = NULL;
  String* filenameOut = NULL;
  String* ligand      = NULL;
  String* name        = NULL;
  String* scheme      = NULL;
  String* selection   = NULL;
  
  if (argc < 7) {
    
    if (MESSAGE) {
      cout << "Error in the input parameters! Must by like: " << endl;
      cout << "./AddModelResult  <Filename with Classifier - In>";
      cout <<                  " <Ligand Name (ID)>";
      cout <<                  " <Classifier Model>";
      cout <<                  " <Scheme>";
      cout <<                  " <Feature Selection Algorithm used>";
      cout <<                  " <Filename with Classifiers - Out>";
      cout << endl;
    }
    
    return 0;
    
  }
  else {
    
    filenameIn  = new String(argv[1]);
    ligand      = new String(argv[2]);
    name        = new String(argv[3]);
    scheme      = new String(argv[4]);
    selection   = new String(argv[5]);
    filenameOut = new String(argv[6]);
    
    if (MESSAGE) {
      cout << "-----------------------------------------------------------------------------------" << endl;
      cout << "  Filename with Classifier Output - In : " << argv[1] << endl;
      cout << "                      Ligand Name (ID) : " << argv[2] << endl;    
      cout << "                      Classifier Model : " << argv[3] << endl;    
      cout << "                                Scheme : " << argv[4] << endl;    
      cout << "      Feature Selection Algorithm used : " << argv[5] << endl;    
      cout << " Filename with Classifier Output - Out : " << argv[6] << endl;    
      cout << "-----------------------------------------------------------------------------------" << endl;
    }
    
  }
  
  ModelsSummaryByLigand* modelsSummaryByLigand = new ModelsSummaryByLigand ();
  
  cout << "Preparing the files..." << endl;
  modelsSummaryByLigand->FilenameIn(filenameOut);
  modelsSummaryByLigand->FilenameOut(filenameOut);
  modelsSummaryByLigand->LigandName(ligand);
  
  if (access(filenameOut->Out(), F_OK) != -1) {
    cout << "Loading classifiers in memory..." << endl;
    if (!modelsSummaryByLigand->LoadXMLFile())
      cout << "Error loading classifiers in memory!" << endl;
  }
  
  cout << "Loading the new classifier data..." << endl;
  if (!modelsSummaryByLigand->AddModelFromFile(filenameIn, name, scheme, selection))
    cout << "Error loading the new classifier data!" << endl;
  
  cout << "Updating the classifiers in a XML file..." << endl;
  if (!modelsSummaryByLigand->WriteXMLFile())
    cout << "Error updating the classifiers in a XML file!" << endl;
  
  cout << "Finished..." << endl << "Good Bye" << endl;
  cout << "-----------------------------------------------------------------------------------" << endl;
  
  if (modelsSummaryByLigand) delete modelsSummaryByLigand;
  
  if (filenameIn)            delete filenameIn;
  if (filenameOut)           delete filenameOut;
  if (ligand)                delete ligand;
  if (name)                  delete name;
  if (scheme)                delete scheme;
  if (selection)             delete selection;

  return EXIT_SUCCESS;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//

